package zmee.wchn;

import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.pmgt.pmbs.common.enums.StatusEnum;
import kd.pmgt.pmbs.common.enums.approval.ReportStatusEnum;

import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述： 设置项目F7列表中过滤掉立项审批中的项目
 *
 * @author GaoFeng
 * @date 2023/4/27 9:29
 */
public class FilterProjectPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        //注册project字段的监听
        BasedataEdit project = this.getControl("project");
        if (project != null) {
            project.addBeforeF7SelectListener(this);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        String propertyKey = beforeF7SelectEvent.getProperty().getName();
        if ("project".equals(propertyKey)) {
            //打开项目F7时设置自定义条件
            ListShowParameter listShowParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            QFilter idListQFilter = new QFilter("id", QCP.not_in, this.getApprovalInProgressProjectList());
            listShowParameter.getListFilterParameter().getQFilters().add(idListQFilter);
        }
    }

    /**
     * 描述：获取项目立项单中需上报单据的上报审批信息不为通过状态的立项，从而获取立项单中关联的主数据项目ID集合
     *
     * @return List<Long> 立项审批中的主数据项目ID集合
     * @author GaoFeng
     * @date 2023/4/27 14:22
     */
    private List<Long> getApprovalInProgressProjectList() {
        //获取立项单中上报为“是”，且“上报审批状态”不为“通过”的单
        QFilter qFilter = new QFilter("repaudstatus", QCP.not_equals, ReportStatusEnum.PASS.getValue());
        qFilter.and("isreport",QCP.equals,true);
        DynamicObjectCollection approvalList = QueryServiceHelper.query("pmas_pro_approval", "id,pro", new QFilter[]{qFilter});
        //获取立项单关联的项目
        return approvalList.stream().filter(item -> item.getLong("pro") != 0L).map(o -> o.getLong("pro")).collect(Collectors.toList());
    }
}